package com.cardinalsolutions.progressindicator.slice;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * CurvedTextView
 *
 * @author sking
 * @since : Jun 23, 2014 11:15:32 AM
 */
public class CurvedTextView extends Text implements Component.DrawTask {
    private Path myArc;
    private Paint mPaintText;

    public CurvedTextView(Context context) {
        super(context);
        init(null, context);
    }

    public CurvedTextView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(attrSet, context);
    }

    public CurvedTextView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(attrSet, context);
    }

    private void init(AttrSet attrs, Context context) {
        this.myArc = new Path();
        // create paint object
        this.mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        // set style
        this.mPaintText.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        // set color
        Color mPaintTextColor = new Color(Color.getIntColor("#ff000000"));
        this.mPaintText.setColor(mPaintTextColor);
        // set text Size
        this.mPaintText.setTextSize(41);
        addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        int centerXOnView = component.getWidth() / 2;
        int centerYOnView = component.getHeight() / 2;

        int viewXCenterOnScreen = getLeft() + centerXOnView;
        int viewYCenterOnScreen = getTop() + centerYOnView;

        float threeDpPad = 12;
        float rad = 193;

        int leftOffset = (int) ((double) viewXCenterOnScreen - ((double) rad + (double) (threeDpPad * 4)));
        int topOffset = (int) ((double) viewYCenterOnScreen - ((double) rad + (double) (threeDpPad * 3)));
        int rightOffset = (int) ((double) viewXCenterOnScreen + ((double) rad + (double) (threeDpPad * 4)));
        int bottomOffset = (int) ((double) viewYCenterOnScreen + ((double) rad + (double) threeDpPad));

        RectFloat oval = new RectFloat(leftOffset, topOffset, rightOffset, bottomOffset);

        int textLength = getText().length();
        if ((textLength % 2) != 0) {
            textLength = textLength + 1;
        }

        this.myArc.addArc(oval, -90 - (textLength * 2), 90 + textLength + 10);

        canvas.drawTextOnPath(this.mPaintText, getText(), this.myArc, 0, 10);
    }

}
